<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-form-item>
          <el-button type="primary" @click="handleDownload">
            导出 Excel
          </el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="handleDownload">
            导出 Excel
          </el-button>
        </el-form-item> -->
        <!-- <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button> -->
        <!-- <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item style="padding-right: 10px">
            <el-select
              v-model="queryForm.invoice_status"
              placeholder="是否开票"
            >
              <el-option value="">全部</el-option>
              <el-option
                v-for="(type, i) in alias.status_alias"
                :key="i"
                clearable="true"
                :label="type"
                :value="i"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.metting_name"
              clearable
              placeholder="请输入会议标题"
              style="padding-right: 10px"
            />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="queryForm.range"
              end-placeholder="创建结束日期"
              range-separator="至"
              start-placeholder="创建开始日期"
              type="daterange"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
      <!-- <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      /> -->
      <el-table-column
        align="center"
        label="订单号"
        prop="order_no"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="会议标题"
        prop="metting_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="票价"
        prop="price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="售价"
        prop="avg_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="票数"
        prop="number"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="总额"
        prop="total"
        show-overflow-tooltip
      />
      <!-- <el-table-column align="center" label="开票状态" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tag :type="row.invoice_alias == '未开票' ? 'danger' : ''">
            {{ row.invoice_alias }}
          </el-tag>
        </template>
      </el-table-column> -->
      <el-table-column
        align="center"
        label="开票状态"
        prop="status"
        show-overflow-tooltip
        width="80"
      >
        <template #default="{ row }">
          <el-tooltip
            v-if="row.invoice_status != 0"
            :content="row.invoice_status === 2 ? '点击已开票' : '点击未开票'"
            :enterable="false"
            placement="top"
          >
            <el-switch
              v-if="row.invoice_status != 0"
              v-model="row.invoice_status"
              :active-value="1"
              :inactive-value="2"
              @change="changeStatus(row.invoice_id)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="支付方式"
        prop="pay_method_alias"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="支付时间"
        prop="pay_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="150"
      >
        <template #default="{ row }">
          <el-button v-if="row.invoice_id" type="text" @click="handleEdit(row)">
            查看开票信息
          </el-button>
          <!-- <el-button type="text" @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <detail ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import {
    defineComponent,
    onMounted,
    reactive,
    toRefs,
    getCurrentInstance,
  } from 'vue'
  import { getList, exportList } from '@/api/order'
  import { doEdit } from '@/api/ordeInvoice'
  import Detail from './component/Detail.vue'

  export default defineComponent({
    name: 'FinanceManagement',
    components: { Detail },

    setup() {
      const { proxy } = getCurrentInstance()

      const state = reactive({
        editRef: null,
        list: [],
        alias: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          range: '',
          metting_name: '',
          status: 2,
        },
        roleList: [],
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        state['editRef'].showEdit(row)
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const changeStatus = async (id) => {
        const { msg } = await doEdit({ id: id })
        proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total, alias },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.alias = alias
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        changeStatus,
      }
    },
    methods: {
      // exportData() {
      //   return await getList(state.queryForm)
      // },
      handleDownload: async function () {
        this.downloadLoading = true
        let exportData = await exportList(this.queryForm)

        import('@/utils/excel').then((excel) => {
          const tHeader = [
            '订单号',
            '会议标题',
            '票价',
            '售价',
            '票数',
            '总额',
            '开票状态',
            '支付方式',
            '支付时间',
          ]
          const filterVal = [
            'order_no',
            'metting_name',
            'price',
            'avg_price',
            'number',
            'total',
            'invoice_alias',
            'pay_method_alias',
            'pay_time',
          ]
          const list = exportData.data.list
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '财务管理表',
            autoWidth: true,
            bookType: 'xlsx',
          })
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map((v) =>
          filterVal.map((j) => {
            return v[j]
          })
        )
      },
    },
  })
</script>
